package com.workcenter.entity;

import com.ppcodes.core.data.DXml;
import com.ppcodes.core.data.DXmlHead;
import com.ppcodes.core.net.HttpClient;
import com.workcenter.entity.model.ERepairsInfo;
import com.workcenter.entity.model.ERepairsState;
import com.workcenter.entity.model.ESearchCondition;

import android.util.Log;

public final class NetClient
{
   private static final int PAGE_SIZE = 20;

   /**
    * 发送
    * 
    * @param sendData
    * @return
    */
   private static DXml sendMessage(DXml sendData)
   {
	  try
	  {
		 String xml = HttpClient.sendMessage(UrlParams.PostURL, sendData.toBytes());
		 if (xml != null && !xml.equals(""))
		 {
			return new DXml(xml);
		 }
		 else
		 {
			return null;
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_sendMessage(sendData) ", e);
		 }
	  }
	  return null;
   }


   /**
    * 
    * @param LoginName
    * @param Password
    * @return
    */
   public static DXml Login(String LoginName, String Password)
   {
	  try
	  {
		 DXml data = new DXml();
		 data.AddHead(new DXmlHead(UrlParams.Login.APICode));
		 data.getBody().AddField(UrlParams.Login.LoginName, LoginName);
		 data.getBody().AddField(UrlParams.Login.Password, Password);

		 return sendMessage(data);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_Login(UserName, Password) ", e);
		 }
	  }
	  return null;
   }
   
   /**
    * 获取当日报修
    * @param UserName
    * @return
    */
   public static DXml GetToadyRepairsList(String UserName)
   {
	  try
	  {
		 DXml data = new DXml();
		 data.AddHead(new DXmlHead(UrlParams.GetToadyRepairsList.APICode));
		 data.getBody().AddField(UrlParams.GetToadyRepairsList.UserName, UserName);

		 return sendMessage(data);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_GetToadyRepairsList(UserName) ", e);
		 }
	  }
	  return null;
   }

   /**
    * 
    * @param CustomerName
    * @param RepairsID
    * @param StartDate
    * @param EndDate
    * @param Progress
    * @return
    */
   public static DXml SearchRepairsList(ESearchCondition sc)
   {
	  try
	  {
		 DXml data = new DXml();
		 data.AddHead(new DXmlHead(UrlParams.SearchRepairsList.APICode));
		 data.getBody().AddField(UrlParams.SearchRepairsList.UserName, Session.getInstance().getUserInfo().getUserName());
		 data.getBody().AddField(UrlParams.SearchRepairsList.CustomerName, sc.getCustomerName());
		 data.getBody().AddField(UrlParams.SearchRepairsList.RepairsId, sc.getRepairsId());
		 data.getBody().AddField(UrlParams.SearchRepairsList.StartDate, sc.getStartDate());
		 data.getBody().AddField(UrlParams.SearchRepairsList.EndDate, sc.getEndDate());
		 data.getBody().AddField(UrlParams.SearchRepairsList.Progress, sc.getProgress().equals("全部")?"":sc.getProgress());

		 return sendMessage(data);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_SearchRepairsList(CustomerName,RepairsID,StartDate,EndDate,Progress) ", e);
		 }
	  }
	  return null;
   }
   
   /**
    * 
    * @param RepairsId
    * @return
    */
   public static DXml GetRepairsDetails(String RepairsId,String RepairsDate)
   {
	  try
	  {
		 DXml data = new DXml();
		 data.AddHead(new DXmlHead(UrlParams.GetRepairsDetails.APICode));
		 data.getBody().AddField(UrlParams.GetRepairsDetails.RepairsId, RepairsId);
		 data.getBody().AddField(UrlParams.GetRepairsDetails.RepairsDate, RepairsDate);

         return sendMessage(data);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_GetRepairsDetails(RepairsId) ", e);
		 }
	  }
	  return null;
   }
   
   /**
    * 
    * @param info
    * @return
    */
   public static DXml InsertNewRepairs(ERepairsInfo info)
   {
	  try
	  {
		 DXml data = new DXml();
		 data.AddHead(new DXmlHead(UrlParams.AddNewRepairs.APICode));
		 data.getBody().AddField(UrlParams.AddNewRepairs.CustomerArea, info.getCustomerArea());
		 data.getBody().AddField(UrlParams.AddNewRepairs.CustomerName, info.getCustomerName());
		 data.getBody().AddField(UrlParams.AddNewRepairs.Diagnosis, info.getDiagnosis());
		 data.getBody().AddField(UrlParams.AddNewRepairs.Instrument, info.getInstrument());
		 data.getBody().AddField(UrlParams.AddNewRepairs.InstrumentID, info.getInstrumentID());
		 data.getBody().AddField(UrlParams.AddNewRepairs.IsStop, info.getIsStop());
		 data.getBody().AddField(UrlParams.AddNewRepairs.Priority, info.getPriority());
		 data.getBody().AddField(UrlParams.AddNewRepairs.RepairsDate, info.getRepairsDate());
		 data.getBody().AddField(UrlParams.AddNewRepairs.RepairsPhone, info.getRepairsPhone());
		 data.getBody().AddField(UrlParams.AddNewRepairs.RepairsStaff, info.getRepairsStaff());
		 data.getBody().AddField(UrlParams.AddNewRepairs.RoomName, info.getRoomName());
		 data.getBody().AddField(UrlParams.AddNewRepairs.UserName,Session.getInstance().getUserInfo().getUserName());

         return sendMessage(data);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_InsertNewRepairs() ", e);
		 }
	  }
	  return null;
   }
   
   /**
    * 
    * @param state
    * @param WorkOrderId
    * @return
    */
   public static DXml InsertNewRepairsState(ERepairsState state,String WorkOrderId)
   {
	  try
	  {
		 DXml data = new DXml();
		 data.AddHead(new DXmlHead(UrlParams.AddNewRepairsState.APICode));
		 data.getBody().AddField(UrlParams.AddNewRepairsState.UserName, Session.getInstance().getUserInfo().getUserName());
		 data.getBody().AddField(UrlParams.AddNewRepairsState.WorkOrderId, WorkOrderId);
		 data.getBody().AddField(UrlParams.AddNewRepairsState.DisposeCondition, state.getDisposeCondition());
		 data.getBody().AddField(UrlParams.AddNewRepairsState.OperatingType,state.getOperatingType());

         return sendMessage(data);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_InsertNewRepairsState ", e);
		 }
	  }
	  return null;
   }
   
   public static DXml SearchCustomers(String CustomerName,String Instrument)
   {
	  try
	  {
		 DXml data = new DXml();
		 data.AddHead(new DXmlHead(UrlParams.SearchCustomer.APICode));
		 data.getBody().AddField(UrlParams.SearchCustomer.CustomerName,CustomerName);
		 data.getBody().AddField(UrlParams.SearchCustomer.Instrument, Instrument);

         return sendMessage(data);
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "NetClient_SearchCustomers(CustomerName, Instrument) ", e);
		 }
	  }
	  return null;
   }
   
   
}
